<template>
  <div id="adjustment" v-loading="loading">
    <v-tableTitle
      :btnConfig="btnConfig"
      :tableSelectData="tableSelectData"
      :title="$t('ex.intertransition')"
      @del="del"
      @newCreate="newCreate"
      @transfer="transfer"
      @handleDownErrorFile="exceptionFileUrl"
      @handleDownTemplate="downloadExcelFile"
    ></v-tableTitle>
    <v-table
      :pageSize="pageSize"
      :page="page"
      :tableConfig="tableConfig"
      :tableData="tableData"
      :total="total"
      @findData="getTableData"
      @getTableData="getTableData"
      @searhData="searhData"
      @selectData="selectData"
      @setdialog="setdialog"
      @upData="getTableData"
      ref="table"
    ></v-table>
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";

export default {
    name: "interior",
    mixins: [baseMixin, config],
    data() {
        return {
            loading: false,
            tableConfig: [
                {
                    label: this.$t('ex.transferkey'),
                    prop: "transferkey",
                    input: "input",
                },
                {
                    label: this.$t('in.externreceiptkey2'),
                    prop: "referencenumber",
                    input: "input",
                },
                {
                    label: this.$t('ex.referencenumber',{no:2}),
                    prop: "referencenumber2",
                    input: "input",
                },
                {
                    label: this.$t('common.type'),
                    prop: "type",
                    input: "select",
                    options: this.getOptions("codelkup/TRANTYPE")
                },
                {
                    label: this.$t('common.status'),
                    prop: "status",
                    input: "select",
                    options: this.getOptions("codelkup/TRANSTATUS")
                },
                {
                    label: this.$t('ex.fromstorerkey'),
                    prop: "fromstorerkey",
                    input: "input",
                },
                {
                    label: this.$t('ex.tostorerkey'),
                    prop: "tostorerkey",
                    input: "input",
                },
                {
                    label: this.$t('ex.qty'),
                    prop: "qty",
                    input: "input",
                },
                {
                    label:  this.$t('app.remark'),
                    prop: "notes",
                    input: "input",
                },
                {
                    label: this.$t('app.effectiveDate'),
                    prop: "effectivedate",
                    input: "date",
                },
                {
                    label: this.$t('ex.transferfinishflag'),
                    prop: "transferfinishflag",
                    input: "input",
                },
                {
                    label: this.$t('app.addDate'),
                    prop: "adddate",
                    input: "date",
                },
                {
                    label: this.$t('app.addName'),
                    prop: "addwho",
                    input: "input",
                },
                {
                    label: this.$t('out.editDate'),
                    prop: "editdate",
                    input: "date",
                },
                {
                    label: this.$t('app.editName'),
                    prop: "editwho",
                    input: "input",
                },
            ],
            tableData: [],
            pageSize: 10,
          page: 1,
            total: 0,
            tableSelectData: [],
            tableSearhData: {},
        };
    },
    computed: {
        btnConfig() {
            return [
              {type: "file", certigier: !this.btnPermNameList.includes('WM06020701'), label: this.$t('common.import'), invalid: "file",importType:'10319',importUrl:"importTransfer",downTemplate:"handleDownTemplate",sheetKey:"rtxTransferDetailList"},
                {label: this.$t('common.create'), certigier: !this.btnPermNameList.includes('WM06020701'), invalid: "newCreate"},
                {label: this.$t('common.delete'),  certigier: !this.btnPermNameList.includes('WM06020702'),disabled: true, invalid: "del"},
                {label: this.$t('ex.transfer'),  certigier: !this.btnPermNameList.includes('WM06020703'),disabled: true, invalid: "transfer"},
            ]
        }
    },
    created() {
    },
    methods: {
      exceptionFileUrl(exceptionUrl) {

        console.log("exceptionFileUrl = %o", exceptionUrl);
        const data = [{
          exceptionFileUrl: exceptionUrl
        }];

        // 异常下载配置
        let urlConf = {responseType: 'blob'};
        let exceptionFileName = "内部转移导入模板异常文件.xls";
        let url = '/comExcelImportResult/downloadExceptionFile';
        let options = {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'};

        try {
          this.$api.post(url, data, urlConf).then(res => {
            let blobParts = [res.data];
            if (window.navigator.msSaveBlob) {
              const blobObject = new Blob(blobParts);
              window.navigator.msSaveBlob(blobObject, exceptionFileName);
            } else {
              let tagName = 'a';
              const blob = new Blob(blobParts, options);
              const downloadElement = document.createElement(tagName);
              // 创建下载的链接
              const href = window.URL.createObjectURL(blob);
              downloadElement.style.display = 'none';
              downloadElement.href = href;
              // 下载后文件名
              downloadElement.download = exceptionFileName;
              document.body.appendChild(downloadElement);
              // 点击下载
              downloadElement.click();
              // 下载完成移除元素
              document.body.removeChild(downloadElement);
              // 释放掉blob对象
              window.URL.revokeObjectURL(href);
            }
          });
        } catch (e) {
          this.$message.error(e);
        }
      },
      async downloadExcelFile(data) {
        try {
          let xlsName = "内部转移导入模板.xls";
          let url = "/downloadExcelFile/" + xlsName;
          let res = await this.$api.post(url, {}, {responseType: 'blob'})
          if (window.navigator.msSaveBlob) {
            var blobObject = new Blob([res.data]);
            window.navigator.msSaveBlob(blobObject, xlsName);
          } else {
            var blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'});
            var downloadElement = document.createElement('a');
            var href = window.URL.createObjectURL(blob); //创建下载的链接
            downloadElement.style.display = 'none';
            downloadElement.href = href;
            downloadElement.download = xlsName; //下载后文件名
            document.body.appendChild(downloadElement);
            downloadElement.click(); //点击下载
            document.body.removeChild(downloadElement); //下载完成移除元素
            window.URL.revokeObjectURL(href); //释放掉blob对象
          }
        } catch (e) {
          this.$message.error(e);
        }
      },

        //转移
        async transfer(data) {
            try {
                let res = await this.$api.post(`/excuteRtxTransfer`, data);
                
                    this.$message.success(res.returnMgr);
                    let page = sessionStorage.getItem('page');
                    this.getTableData(page, this.tableSearhData);
                    
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        //新建
        async newCreate() {
            this.$store.commit("setNewFlag", true);
            this.$router.push({name: 'interiorDeawer'});
        },
        //选中的内容
        selectData(data) {
            this.tableSelectData = data;
        },
        //删除
        async del(data) {
            try {
                let res = await this.$api.post(`/deleteRtxTransfer`, data);
                
                    this.$message.success(res.returnMgr);
                    let page = sessionStorage.getItem('page');
                    this.getTableData(page, this.tableSearhData);
                    
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        async getTableData(page, data) {
            try {
                this.loading = true;
                let res = await this.$api.post(`/queryRtxTransferList/${page}`, data);
                
                    this.tableData = res.page.list;
                    this.pageSize = res.page.pageSize;
                  this.page = res.page.curPage;
                    this.total = res.page.total;
                
            } catch (e) {
                this.$message.error(e);
            } finally {
                this.loading = false;
            }
        },
        //搜索的值
        searhData(data, up) {
            if (!up) {
                this.tableSearhData = data;
            } else {
                this.tableSearhData = data;
                this.getTableData(1, this.tableSearhData);
            }
        },
        //设置弹框
        setdialog(data) {
            this.$store.commit("setRouteData", data);
            this.$router.push({name: 'interiorDeawer'});
        },
    },
    mounted() {
        // 
        this.$store.commit("setSkip", {});
        this.$store.commit("setNewFlag", false);
        this.$store.commit("setRouteData", {});
        this.getTableData(1, {});
    },
    watch: {},
    components: {
        "v-tableTitle": tableTitle,
        "v-table": table,
    }
};
</script>

<style scoped>
#adjustment {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
